#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5,M = 18,INF = 1e9;
template <typename T> void tomin(T& x,T y){x = min(x,y);}
template <typename T> void tomax(T& x,T y){x = max(x,y);}
//
int T,rx[4];
string d[M];
int n,m,k,a[N],ra[N],c[N],qk[N];

int L[N],R[N],lc[N],rc[N],dep[N],rk[N],buc[M],pre[M],tot;

int build(int l,int r,int d){
    int x = ++tot;
    L[x] = l,R[x] = r; 
    dep[x] = d,rk[x] = buc[d]++;
    if(l==1)pre[d] = x;
    //
    if(l==r)return x;
    int mid = (l+r)>>1;
    lc[x] = build(l,mid,d-1),rc[x] = build(mid+1,r,d-1);
    return x;
}
//
int p[N]; ll sum[M][N];

void dfs(int u){
    if(L[u] > n || L[u] == R[u])return;
    int x = lc[u],y = rc[u];
    dfs(x); dfs(y); if(d[dep[u]][rk[u]] == '1')swap(x,y);
    //
    int mx = 0;
    for(int i=L[x];i<=R[x];i++){
        if(p[i] < i)tomax(mx,p[i]);
        else{
            if(a[i] < dep[u])tomax(mx,p[i]);
            else tomax(mx,i-1);
        }
        if(p[i] >= i && a[i] < dep[u])p[i] = i-1;
    }
    for(int i=L[y];i<=R[y];i++)tomin(p[i],mx);
    //
    if(L[u] == 1){
        for(int i=L[u];i<=R[u];i++)sum[dep[u]][min(1<<dep[u],p[i])] += i;
    }
}

void solve(){
    for(int i=0;i<4;i++)cin>>rx[i];
    for(int i=1;i<=n;i++)a[i] = ra[i] ^ rx[i%4];
    for(int i=0;i<=k;i++)for(int j=0;j<=(1<<i);j++)sum[i][j] = 0;
    //
    for(int i=1;i<=(1<<k);i++)p[i] = n; sum[0][1] = 1;
    dfs(1);
    for(int i=0;i<=k;i++)for(int j=(1<<i)-1;j>=0;j--)sum[i][j] += sum[i][j+1];
    //
    ll ans = 0;
    for(int i=1;i<=m;i++){
        ll val = sum[qk[i]][c[i]];
        ans ^= val * i;
    }
    cout<<ans<<endl; 
}

int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    
    ios::sync_with_stdio(false); cin.tie(0);

    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>ra[i];
    for(int i=1;i<=m;i++){
        cin>>c[i];
        while((1<<qk[i]) < c[i])qk[i]++;
    }
    while((1<<k) < n)k++;
    for(int i=1;i<=k;i++)cin>>d[i];
    build(1,1<<k,k);
    //
    cin>>T;
    while(T--)solve();

    return 0;
}